<?php

/**
 * This is the model class for table "{{document}}".
 *
 * The followings are the available columns in table '{{document}}':
 * @property integer $ID
 * @property integer $topicID
 * @property string $name
 * @property string $description
 * @property string $content
 * @property integer $photo
 * @property integer $createddate
 * @property integer $modifieddate
 * @property string $enabled
 * @property integer $deleted
 */
class Document extends CActiveRecord
{
  public $subjectID;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Document the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{document}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, description, content', 'required'),
			array('createddate, modifieddate, enabled, deleted', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('description', 'length', 'max'=>500),
			array('enabled', 'length', 'max'=>3),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, name, description, content, photo, createddate, modifieddate, enabled, deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'ID',
		  'subjectID'=>'Môn học',
		  'topicID'=>'Chuyên đề',
			'name' => 'Tiêu đề',
			'description' => 'Mô tả',
			'content' => 'Nội dung',
			'photo' => 'Hình ảnh',
			'createddate' => 'Ngày tạo',
			'modifieddate' => 'Ngày cập nhật',
			'enabled' => 'Cho phép',
			'deleted' => 'Deleted',
		);
	}

  protected function beforeSave() {
		if($this->isNewRecord) {
			$this->createddate = time();
		}
			$this->modifieddate = time();
		return parent::beforeSave();
	}
	
	/**
	 * get Enabled @author K.D
	 */
	
	public function getEnabled() {
	  if($this->enabled==1)
	    return "Hiện";
	  else
	    return "Ẩn";
	}
	/**
	 * return photo of document
	 * @author K.D
	 */
  public function getPhoto($docID) {
    $image = "";
    $doc = Document::model()->findByAttributes(array('ID'=>$docID,'deleted'=>0));
    if(!empty($doc) && $doc->photo!=0) {
      $file = File::model()->findByPk($doc->photo);
      if(!empty($file) && file_exists(Yii::app()->basePath.'/../upload/document/thumbs_100x100/'.$file->filesaved)) {
        $image = $file->filesaved;
      }
    }
    return $image;
  }
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
    $criteria->together = true;
    $criteria->select = 't.*';
    $criteria->join = 'JOIN tbl_topic tp ON tp.ID=t.topicID';
		$criteria->condition = "t.deleted=0";
		
		$criteria->compare('t.name',trim($this->name),true);
		$criteria->compare('tp.name',trim($this->topicID),true);
		$criteria->compare('description',trim($this->description),true);
		$criteria->compare('enabled',$this->enabled,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		  'sort'=>array(
        'attributes'=>array(
            'topicID'=>array(
                'asc'=>'tp.name',
                'desc'=>'tp.name DESC',
            ),
            '*',
        ),
   		),
		));
	}
  public function search_doc()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
    $criteria->together = true;
    $criteria->select = 't.*, sub.subject, gr.grade grade';
    $criteria->join = 'JOIN tbl_topic tp ON tp.ID=t.topicID JOIN tbl_subject sub ON sub.ID=tp.subjectID
    									JOIN tbl_grade gr ON gr.ID=tp.gradeID';
		$criteria->condition = "t.deleted=0 AND tp.deleted=0 AND sub.deleted=0 AND gr.deleted=0";
		$criteria->order = "sub.ID, tp.createddate DESC";
		
		$criteria->compare('t.name',trim($this->name),true);
		$criteria->compare('tp.name',trim($this->topicID),true);
		$criteria->compare('description',trim($this->description),true);
		$criteria->compare('enabled',$this->enabled,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}